import java.io.*;
import java.net.Socket;

public class ClientTest {
    public static void main(String[] args) {
        Socket s = null;
        OutputStream os = null;
        ObjectOutputStream oos = null;
        InputStream is = null;
        BufferedReader br = null;
        try {
            // 1.创建Socket类型的对象并提供服务器的主机名和端口号
            s = new Socket("127.0.0",8888);
            System.out.println("连接服务器成功！");

            // 2.获取该Socket的输出流，用来向服务器发送对象
            os = s.getOutputStream();
            oos = new ObjectOutputStream(os);
            oos.writeObject(new UserMessage("check", new User("admin", "123456")));
            //s.shutdownOutput();
            String infoString = null;

            //获取输入流，取得服务器的信息
            is = s.getInputStream();
            br = new BufferedReader(new InputStreamReader(is));
            String info = br.readLine();
            System.out.println(("success".equals(info)) ? "登录成功！" : "登录失败！");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != br) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != oos) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != os) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != s) {
                try {
                    s.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
